package com.jwthhealth_pub.old.report.view.widget.linechart;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jwthhealth_pub.R;


public class JwthLineChart extends View {

    private Paint linePaint, imaginaryPaint;
    private int mParentWidth;
    private int mparentHeight;
    private float targetX, targetY;

    public void setTArgetLoa(float x, float y) {
        Log.d("LikeLineChart", "y:" + y);

        targetX = x;
        if (y<0) {
            targetY = Math.abs(y) + 30;//  30 坐标补偿
        } else {
            targetY = 30 - Math.abs(y);
        }
        Log.d("LikeLineChart", "targetX:" + targetX);
        Log.d("LikeLineChart", "targetY:" + targetY);
        invalidate();
    }

    public JwthLineChart(Context context) {
        super(context);
        init();
    }

    public JwthLineChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mParentWidth = MeasureSpec.getSize(widthMeasureSpec);
        mparentHeight = MeasureSpec.getSize(heightMeasureSpec);
        this.setMeasuredDimension(mParentWidth, mparentHeight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(1);
        linePaint.setColor(Color.parseColor("#777777"));

        setBackgroundColor(Color.WHITE);

        //绘制网格线
//        final int width = 1000;
        final float width = mParentWidth;
//        final int height = 700;
//        final int height = mparentHeight;
//        final int edgeWidth = 10;
//        final int space = 100;   //长宽间隔
        final int heightSpace = mparentHeight/9;   //长宽间隔
        final int widthSpace = mParentWidth/10;   //长宽间隔
//        final int phStartY, phEndX, phEndY, phYSpace=20;
//        int vertz = 100;
//        int hortz = 100;
        //精确测量用float类型
        float vertz = mparentHeight/9;
        float hortz = mParentWidth/10;

        // 绘制网格

//        PathEffect effects = new DashPathEffect(new float[]{1,1,1,5},1);
//        linePaint.setPathEffect(effects);

        for(int i=0;i<10;i++){
            if (i<=6)
                canvas.drawLine(mParentWidth/10,  vertz,  width, vertz, imaginaryPaint);

            if (i==9 && hortz<width)
                hortz = width;
            canvas.drawLine(hortz, mparentHeight/9, hortz, mparentHeight/9*7, imaginaryPaint);
            vertz+=heightSpace;
            hortz+=widthSpace;

        }

        //绘制ph值基线
        linePaint.setStrokeWidth(2);
//        canvas.drawLines(new float[]{
//                100, 473, 441, 100,
//                100, 507, 555, 100,
//                100, 538, 720, 100,
//                100, 567, 931, 100,
//                100, 594, 1000, 192,
//                100, 618, 1000, 299,
//                100, 637, 1000, 388,
//                100, 655, 1000, 456,
//        }, linePaint);

        float _unitHeight = mparentHeight/9;

        canvas.drawLines(new float[]{
                mParentWidth/10, 473f/100f*_unitHeight, 441f/100f*mParentWidth/10, _unitHeight,
                mParentWidth/10, 507f/100f*_unitHeight, 555f/100f*mParentWidth/10, _unitHeight,
                mParentWidth/10, 538f/100f*_unitHeight, 720f/100f*mParentWidth/10, _unitHeight,
                mParentWidth/10, 567f/100f*_unitHeight, 931f/100f*mParentWidth/10, _unitHeight,
                mParentWidth/10, 594f/100f*_unitHeight, mParentWidth, 192f/100f*_unitHeight,
                mParentWidth/10, 618f/100f*_unitHeight, mParentWidth, 299f/100f*_unitHeight,
                mParentWidth/10, 637f/100f*_unitHeight, mParentWidth, 388f/100f*_unitHeight,
                mParentWidth/10, 655f/100f*_unitHeight, mParentWidth, 456f/100f*_unitHeight,
        }, imaginaryPaint);

        linePaint.setStrokeWidth(1);
        //绘制横坐标
        linePaint.setTextSize(20);

        float[] ipco2 = new float[] {2.7f, 4.0f, 5.3f, 8.7f, 8.0f, 8.3f, 10, 7f};

        //坐标
        for (int i=0; i<7; i++) {
            canvas.drawText((30-i*10)+"", mParentWidth/10f/2f, (i+1)*mparentHeight/9f, linePaint);
            if (i<6)
                canvas.drawText(((i+2)*10)+"", (i+2)*mParentWidth/10f,  750f/100f*mparentHeight/9f, linePaint);
            else
                canvas.drawText("00", (i+2)*mParentWidth/10f,  750f/100f*mparentHeight/9f, linePaint);

            canvas.drawText(ipco2[i]+"", (i+2)*mParentWidth/10f, 790f/100f*mparentHeight/9f, linePaint);
        }


        //坐标名称
        linePaint.setTextSize(20);
        canvas.drawText("isbe", mParentWidth/100f/2f-5, 350f/100f*mparentHeight/9, linePaint);
        canvas.drawText("mM", mParentWidth/100f/2f-5, 390f/100f*mparentHeight/9, linePaint);
        canvas.drawText("mmhg", 9*mParentWidth/10f, 750f/100f*mparentHeight/9f, linePaint);
        canvas.drawText("kPa", 9*mParentWidth/10f, 790f/100f*mparentHeight/9f, linePaint);
        linePaint.setTextSize(40);
        canvas.drawText("iPCO2", mParentWidth/10/2, 790f/100f*mparentHeight/9f, linePaint);

        //标准框
//        linePaint.setColor(getResources().getColor(R.color.warn_line));
        linePaint.setColor(ContextCompat.getColor(getContext(), R.color.warn_line));
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(5);
        canvas.drawRect(426f/100f*mParentWidth/10, 380f/100f*mparentHeight/9, 513f/100f*mParentWidth/10, 420f/100f*mparentHeight/9, linePaint);

        //测量位置
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bead);
        canvas.drawBitmap(bitmap, (targetX/10)*mParentWidth/10f-25, targetY * mparentHeight/90f + mparentHeight/9-25, linePaint);

    }

    private void init(){
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速, 不关闭无法绘制虚线

        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(2);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setColor(Color.parseColor("#cccccc"));

        //虚线
        imaginaryPaint = new Paint();
        imaginaryPaint.setColor(Color.parseColor("#cccccc"));
//        imaginaryPaint.setStyle(Paint.Style.STROKE);
        imaginaryPaint.setAntiAlias(true);
        imaginaryPaint.setStrokeWidth(1);
        //float数组,必须是偶数长度,且>=2,指定了多少长度的实线之后再画多少长度的空白,
        // 例如{2,2},绘制长度为2的实线，再绘制长度为2的空白,第二个参数代表偏移量
        imaginaryPaint.setPathEffect(new DashPathEffect(new float[] {10, 5}, 0));
    }
}
